package com.bootcamp.mall.utils;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * @program: mall
 * @description: parse json from request to string
 * @author: Keyu Li
 * @create: 2021-11-09 15:29
 **/

public class JSONUtil {
    public static String parseContent(HttpServletRequest request){
        ServletInputStream inputStream = null;
        String jsonContent = null;
        try {
            inputStream = request.getInputStream();
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            byte[] bytes = new byte[1024];
            int length;
            while ((length = inputStream.read(bytes))!=-1){
                outputStream.write(bytes,0,length);
            }
            inputStream.close();
            jsonContent = outputStream.toString("utf-8");
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return jsonContent;
    }
}
